package com.okay.popwindow

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.okay.testclass.R

class PopRecyclerAdapter(recyclerView: RecyclerView) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

   private var selectedId=-1//当前选中的item

    //定义四种常量  表示四种条目类型
    private val TYPE_NONE = -1
    private val TYPE_TITLE = 1
    private val TYPE_CONTENT = 2

    var mContext: Context? = null
    private var listData: MutableList<ClassRelation> = ArrayList()

    init {
        mContext = recyclerView.context
    }

    fun setData(list: List<ClassRelation>) {
        list?.let {
            listData.clear()
            listData.addAll(list)
            notifyDataSetChanged()
        }
    }
    fun getDataList():List<ClassRelation>{
        return listData
    }


    override fun getItemViewType(position: Int): Int {
//        var type = listData.get(position).type
        var type = 2

        when (type) {
            TYPE_TITLE -> return TYPE_TITLE
            TYPE_CONTENT -> return TYPE_CONTENT
        }
        return TYPE_NONE
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        when (viewType) {
            TYPE_TITLE -> {
                val view = LayoutInflater.from(mContext).inflate(R.layout.item_pop_title, parent, false)
                return RecyclerTitleHolder(view)
            }
            TYPE_CONTENT -> {
                val view = LayoutInflater.from(mContext).inflate(R.layout.item_pop_content, parent, false)
                return RecyclerHolder(view)
            }
        }
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_pop_content, parent, false)
        return RecyclerHolder(view)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            TYPE_TITLE -> {
                holder as RecyclerTitleHolder
//                holder.textView.setOnClickListener(getOnClickListener(position))
                holder.textView.text = listData[position].class_name
            }
            TYPE_CONTENT -> {
                holder as RecyclerHolder
                holder.popLayout.setOnClickListener(getOnClickListener(position))
                holder.popClass.text = listData[position].class_name
                holder.popId.text = listData[position].sn
//                (holder as RecyclerHolder).popOnline.setText(listData.get(position).userName)
                if(selectedId==position) {
                    mContext?.let { ContextCompat.getColor(it, R.color.FFE6F2FF) }
                        ?.let(holder.popLayout::setBackgroundColor)
                }else {
                    mContext?.let { ContextCompat.getColor(it, R.color.white) }
                        ?.let(holder.popLayout::setBackgroundColor)
                }
                if(listData[position].onLine) {
                    holder.popOnline.setImageResource(R.drawable.online_bg_1)
                }     else{
                    holder.popOnline.setImageResource(R.drawable.online_bg_2)
                }

            }

        }
    }

    override fun getItemCount(): Int {
        return listData.size
    }

    internal class RecyclerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var popClass: TextView
        var popId: TextView
        var popOnline: ImageView
        var popLayout: RelativeLayout

        init {
            popClass = itemView.findViewById<View>(R.id.id_item_pop_content_class) as TextView
            popId = itemView.findViewById<View>(R.id.id_item_pop_content_id) as TextView
            popOnline = itemView.findViewById<View>(R.id.id_item_pop_content_online) as ImageView
            popLayout = itemView.findViewById<View>(R.id.id_item_pop_content_lay) as RelativeLayout
        }
    }

    internal class RecyclerTitleHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var textView: TextView

        init {
            textView = itemView.findViewById<View>(R.id.id_item_pop_title) as TextView
        }
    }

    private var mListener: onRecyclerItemClickerListener? = null

    /**
     * 增加点击监听
     */
    fun setItemListener(mListener: onRecyclerItemClickerListener?) {
        this.mListener = mListener
    }

    private fun getOnClickListener(position: Int): View.OnClickListener? {
        return View.OnClickListener { v ->
            if (null != mListener && null != v) {
                selectedId = position
                notifyDataSetChanged()
                mListener?.onRecyclerItemClick(v, listData?.get(position), position)
            }
        }
    }

    /**
     * 点击监听回调接口
     */
     interface onRecyclerItemClickerListener {
        fun onRecyclerItemClick(view: View?, data: Any?, position: Int)
    }


}